﻿<?php
/**
 * Classe Db da tabela TB_PARAM_CLASSE
 * 
 * @author			Mário R. Castro
 * @copyright		Voitech Sistema
 * @package			SEMAS
 * @subpackage		semas.application.models.Db
 * @version			1.0
*/
class App_Model_Db_ParamClasse extends Voitech_Db{

	protected $_name 		= 'tb_param_classe';
    protected $_primary 	= 'co_classe';
	
	static private $instancia;

	/**
	 * @return App_Model_Db_ParamClasse
	 */
	static public function build() {
		if( !isset( self::$instancia ) ) {
			$classe = __CLASS__;
			self::$instancia = new $classe();
		}
		return self::$instancia;
	}
	
	public function listarTodos(Voitech_TO $obTO=null){
            $obSelect = $this->getAdapter()->select()
			            				   ->from(array('tb_param_classe'=>'tb_param_classe'),
			            				 		   array(
			            				 		   			'*',
			            				 		   			'fl_excluir' => '(CASE 
																			    WHEN (SELECT count(*) FROM tb_obito WHERE tb_obito.co_classe = tb_param_classe.co_classe) > 0 THEN
																			    	\'N\'
																			    ELSE
																			    	\'S\'
																		  END)'
			            				 		   		));

            if( !empty( $obTO->co_classe ) ) {
                $obSelect->where('co_classe =?', $obTO->co_classe);
            }
            if( !empty( $obTO->no_param_classe ) ) {
                $obSelect->where('no_classe like ?', "%".$obTO->no_classe."%");
            }
            if( !empty( $obTO->st_status ) ) {
                $obSelect->where('st_status =?', $obTO->st_status);
            }
            return $this->getAdapter()->fetchAll($obSelect);			   						   
	}	
	
}